//
//  ComposeView.swift
//  BQWB
//
//  Created by laibingqiang on 16/10/31.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

import UIKit

class ComposeView: UIView {

    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: BQSCREENW, height: BQSCREENH))
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        backgroundColor = UIColor.red
        addSubview(bgimgV)
        addSubview(sloganV)
        
        sloganV.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            
            make.top.equalTo(self).offset(100)
        }
        
        addChildBtn()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.removeFromSuperview()
    }
    
    //懒加载控件
    //屏幕截图
    fileprivate lazy var bgimgV:UIImageView = {
        
        let img:UIImage = self.getScreenImg().applyLightEffect()
        let imgV = UIImageView(image:img )
        
        return imgV
    }()
    //标语图片
    fileprivate lazy var sloganV:UIImageView = {
        
        let imgV:UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
        
        return imgV
    }()
    
    //加载按钮
    private func addChildBtn(){
        //按钮宽
        let btnW:CGFloat = 80
        //按钮高
        let btnH:CGFloat = 110
        //按钮间距
        let margin:CGFloat = (width - btnW * 3) / CGFloat(4)
        
        for i in 0..<6{
            //列数
            let col = i % 3
            //行数
            let row = i / 3
            
            let btn:ComposeBtn = ComposeBtn()
            
            btn.setImage(UIImage(named:"tabbar_compose_friend"), for: UIControlState.normal)
            btn.setTitle("你好", for: UIControlState.normal)
            
            btn.width = btnW
            btn.height = btnH
            btn.x = CGFloat(col) * btnW + CGFloat(col + 1) * margin
            
            btn.y = CGFloat(row) * btnH 
            
            addSubview(btn)

        }
        
    }
    
    //fileprivate lazy var btn:ComposeBtn = ComposeBtn()
    //截屏方法
    private func getScreenImg()->UIImage{
        
        //获取当前主window
        let window:UIWindow = UIApplication.shared.keyWindow!
        //1.开启图形上下文
        UIGraphicsBeginImageContext(window.size)
        
        //绘制区域
        let rect = CGRect(origin: CGPoint.zero, size: window.size)
        
        window.drawHierarchy(in: rect, afterScreenUpdates: false)
        
        let img = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭图形上下文
        UIGraphicsEndImageContext()
        
        return img!
    }
}
